local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local HomeGui=UIManager.ui_list.HomeGui;
local SpinGui=UIManager:CreateUI("SpinGui",require(script.SpinGui))
local Spin_Cfg=ClientOHOH.config.Spin;
local ItemTool=_G.CommonOHOH.load.ItemTool();
local PetTool=_G.CommonOHOH.load.PetTool();
local LanguageHelper=_G.LanguageHelper;
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local EventList = ClientOHOH.load.EventList();
local FunctionList = ClientOHOH.load.FunctionList();
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();
local Good_Cfg=ClientOHOH.config.Goods;
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.dataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.data = self.dataBase:GetData("spinReward");
	self.Level=self.dataBase:GetData("base").level;
	self.IsSpin=false;
	self:FullReward();
	self:SetBuySpinBtn();
	self:SetSpinBtn();
	self:GetFreeSpin();
	CallBackManager:AddRbxConnect(self,HomeGui.insts.Spin.button.Activated, function()
        SpinGui:Show();
	end);
	CallBackManager:AddRbxConnect(self,SpinGui.insts.Spin.Activated, function()	
		if self.data.spinTimes>0 then
			if self.IsSpin then
				return;
			end
			self.IsSpin=true;
			local rewardIndex=Spin_Cfg.RewardSetting[self.data.rewardSettingStep];
			SpinGui:PlaySpin(rewardIndex);
        	task.wait(3.3);
			self:Fire(EventList.start_spin);
			self:ShowSpinReward(rewardIndex);
			else
				self:BuySpinTimes(Spin_Cfg.BuySpinInfo[1][1]);
		end
		
	end);
	CallBackManager:AddRbxConnect(self,SpinGui.insts.LeftSpinBuy.Activated, function()
		self:BuySpinTimes(Spin_Cfg.BuySpinInfo[2][1]);
	end);
	CallBackManager:AddRbxConnect(self,SpinGui.insts.RightSpinBuy.Activated, function()
		self:BuySpinTimes(Spin_Cfg.BuySpinInfo[3][1]);
	end);
    self.SpinFinishConnect=SpinGui.SpinFinishAction:Connect(function()
		self.IsSpin=false;
	end)
	self.parent.UserDataEvent:Connect(function(data)
        if(data["spinReward"])then
            self.data = self.dataBase:GetData("spinReward");
			self:GetFreeSpin();
			self:SetSpinBtn();
        end
        if data["base"] and data["base"].level~=self.Level then
            self.Level=data["base"].level;
			self:FullReward();
		end;
    end);
end;
function M.FullReward(self)
	local rewards=Spin_Cfg.Reward;
	for k, v in pairs(rewards) do
		local key,id=ItemTool.GetKeyAndID(v.item_id);
		local itemId,count=ItemTool.GetRewardByLevel(v.item_id,v.count,self.Level);
		-- if key=="pets" or key=="egg"then
		-- 		v["model"]=ItemTool.GetItemShowView(itemId);
		-- 	else
		-- 		v["icon"]=ItemTool.GetItemShowView(itemId);
		-- end;
		v.icon=ItemTool.GetItemShowView(itemId);
		v.Nowcount=count;
		--v.name=LanguageHelper:GetText(v.name);
	end
	SpinGui:FullRewardView(rewards);
end
function M.SetBuySpinBtn(self)
	SpinGui:SetBuySpinBtnView(Good_Cfg[Spin_Cfg.BuySpinInfo[2][1]].price,Spin_Cfg.BuySpinInfo[2][2],Good_Cfg[Spin_Cfg.BuySpinInfo[3][1]].price,Spin_Cfg.BuySpinInfo[3][2]);
end

function M.SetSpinBtn(self)
	local  spinBtnText=LanguageHelper:GetText(10504);
	local  singlePrice="R$" .. Good_Cfg[Spin_Cfg.BuySpinInfo[1][1]].price;
	local btnText=spinBtnText;
	if(self.data.spinTimes==0)then
		btnText=singlePrice;
	end
	SpinGui:SetSpinBtnView(btnText,self.data.spinTimes);
end
function M.BuySpinTimes(self,goodsId)
	--  if self:Invoke(FunctionList.buy_goods,goodsId) then
	-- 	GlobalTipGui:Show(LanguageHelper:GetText(10506));
	--  end
	self.parent.RobuxBuyEvent:Action(goodsId)
	
end;
--免费获取转盘次数
function M.GetFreeSpin(self)
	local startTime=self.data.lastSpinTime;
	local endTime=os.time();
	local needIntervalTime=Spin_Cfg.IntervalTime[self.data.intervalTimeStep];--需要间隔的时间  
	local intervalTime= os.difftime(endTime, startTime);
	if(intervalTime>=needIntervalTime)then
		--免费增加一次 更新
		self:Fire(EventList.spin_AddFreeTimes);
		else
			--开始倒计时
			--还需要的时间
			self.needTime= startTime + needIntervalTime-endTime;
			self:StartCountDownTime();
	end
end;
function M.ShowSpinReward(self,rewardIndex)
    -- warn("========Spin========>:",item_id,count)
	local item_id,count=ItemTool.GetRewardByLevel(Spin_Cfg.Reward[rewardIndex].item_id,Spin_Cfg.Reward[rewardIndex].count,self.Level);
    ItemTool.FlyingItem(item_id,count)
    Client_AudioManager:PlayGloble("claim");
end
function M.StartCountDownTime(self)
	if self.updateConnect then
        return; 
    end
    self.updateConnect=self:AddStepUpdate(self.Update,1);
end;
function M.Update(self,dt)
	local min=math.huge;
	self.needTime=self.needTime-1;
	min=self.needTime;
	local timeStr=GeneralTool.secondsToHHMM(min);
	HomeGui:UpdateSpinInfo(self.data.spinTimes,timeStr)
    SpinGui:UpdateSpinInfo(timeStr);
	if(self.needTime==0)then
		self.updateConnect:Disconnect();
		self.updateConnect=nil;
		self:Fire(EventList.spin_AddFreeTimes);
	end
end;
function M.dtor(self)
	if self.SpinFinishConnect then
		self.SpinFinishConnect:Disconnect();
		self.SpinFinishConnect=nil;
	end
	M.super.dtor(self);
end;
return M;